<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Request;

class Admin extends Backend
{
    public function __construct()
    {
        parent::__construct();
        $this->AdminModel = model('Admin');
    }

    public function index()
    {

        // 获取管理员数据  分页
        $AdminList = model('Admin')->paginate(2, false, ['query' => Request::instance()->param()]);

        // 条件搜索
        if ($this->request->isGet()) {
            $action = $this->request->param('action');
            if ($action == 'search') {
                $params = $this->request->param();
                if ($params['search'] == 'username') {
                    $AdminList = $this->AdminModel->where('username', 'LIKE','%'.$params['title'].'%')->paginate(4, false, 
                    ['query' => Request::instance()->param()]);
                } 
            }
        }
        // halt($state);
        $navTitle = '管理员管理';
        $navSmall = '管理员列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'AdminList' => $AdminList,
        ]);
    }

    // 新增管理员
    public function add()
    {

        if ($this->request->isPost()) {
            $params = $this->request->param();
            $aname = model('admin')->where('username', $params['adminname'])->find();
            if ($aname) {
                $this->error('该管理员已存在', null, null, 2);
            }
            if ($params['adminname'] == null) {
                $this->error('请输入管理员名称');
            }
            if (($params['password'] == null) && ($params['repassword'] == null)) {
                $this->error('请输入密码');
            }

            if ($params['password'] != $params['repassword']) {
                $this->error('密码不一致');
            }

            $salt = build_ranstr();

            $pwd = md5($params['password'] . $salt);

            $data = [
                'username' => $params['adminname'],
                'nickname' => $params['nickname'],
                'password' => $pwd,
                'salt' => $salt,
                'state' => $params['state']
            ];
            // halt($data);
            $res = model('admin')->save($data);
            if (!$res) {
                $this->error('添加管理员失败');
            }
            $this->success('添加管理员成功', url('admin/admin/index'));
        }
        return $this->fetch();
    }

    // 删除管理员
    public function del()
    {
        $adminid = $this->request->param('id');
        $admin = model('Admin')->find($adminid);
        if ($admin == null || $admin['id'] == $this->LoginAdmin['id']) {
            $this->error('该管理员无法删除');
        }
        $res = model('admin')->destroy($adminid);
        if (!$res) {
            $this->error('删除管理员失败');
        }
        $this->success('删除管理员成功', url('admin/admin/index'), null, 2);
    }

    // 编辑管理员
    public function edit()
    {
        $id = $this->request->param('id');
        // halt($id);
        $admin = model('Admin')->find($id);
        // halt($admin);
        if ($admin == null) {
            $this->error('该管理员不存在，无法更新');
        }

        if ($this->request->isPost()) {
            $params = $this->request->param();
            // halt($params);
            $data = [];
            if ($params['password'] == '' || $params['repassword'] == '') {
                // halt($params['nickname']);
                $data = [
                    'id' => $id,
                    'username' => $params['adminname'],
                    'nickname' => $params['nickname'],
                    'password' => $admin['password'],
                    'salt' => $admin['salt'],
                    'state' => $params['state']
                ];
            } else {
                $salt = build_ranstr();
                $pwd = md5($params['password'] . $salt);
                $data = [
                    'id' => $id,
                    'username' => $params['adminname'],
                    'nickname' => $params['nickname'],
                    'password' => $pwd,
                    'salt' => $salt,
                    'state' => $params['state']
                ];
            }
            // halt($data);
            $res = $this->AdminModel->isUpdate(true)->save($data);
            if (!$res) {
                $this->error($this->AdminModel->getError());
            }
            $this->success('更新管理员成功', url('admin/admin/index'), null, 2);
        }
        return $this->fetch('', [
            'admin' => $admin
        ]);
    }

    // 批量启用
    public function stateUp () {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'stateup') {
                $list = $this->request->param();
                $data = [];
                foreach($list['list'] as $key => $item) {
                    $arr['id'] = $item;
                    $arr['state'] = 1;
                    array_push($data, $arr);
                }
                $res = $this->AdminModel->isUpdate(true)->saveAll($data);
                // halt($res);
                if ($res == false) {
                    $this->error($this->AdminModel->getError());
                }
                $this->success('批量启用成功', url('admin/admin/index'), null, 2);
            }
        }

    }

    // 批量禁用
    public function stateDown() {
        $action = $this->request->param('action');
            if ($action == 'stateDown') {
                $list = $this->request->param();
                $data = [];
                foreach($list['list'] as $item) {
                    $arr['id'] = $item;
                    $arr['state'] = 0;
                    array_push($data, $arr);
                }
                // halt($data);
                if (in_array($this->LoginAdmin['id'], $list['list'])) {
                    $this->error('不能禁用自己', null, null, 2);
                }
                $res = $this->AdminModel->isUpdate(true)->saveAll($data);
                // halt($res);
                if ($res == false) {
                    $this->error($this->AdminModel->getError());
                }
                $this->success('批量禁用成功', url('admin/admin/index'), null, 2);
            }
    }

    // 批量删除
    public function delAll() {
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $ids = implode(',', $params['list']);
            // halt($ids);
            if (in_array($this->LoginAdmin['id'], $params['list'])) {
                $this->error('不能删除自己', null, null, 2);
            }
            $res = $this->AdminModel->where("id in ($ids)")->delete();
            if (!$res) {
                $this->error($this->AdminModel->getError());
            } 
            $this->success("批量删除成功", url('admin/admin/index'), null, 2);
        }
    }
}
